
package com.probiz.estore.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.customer.ValidationSession;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.core.util.ContextUtil;
import com.probiz.estore.customer.dao.ValidationSessionDao;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.customer.service.ValidationSessionManager;
import com.probiz.estore.system.util.TranslateService;

/**
 * ValidationSession Business Delegate (Proxy) implementation to handle
 * communication between web and persistence layer. Implementation of
 * ValidationSessionManager interface. Developer introduced interfaces should be
 * declared here. Won't get overwritten.
 */
public class ValidationSessionManagerImpl extends GenericManagerImpl<ValidationSession> 
implements	ValidationSessionManager {
	
	private ValidationSessionDao 	validationSessionDao 	= null;
	
	@Override
	protected void initManager() {
		this.dao = validationSessionDao;
	}

	@Override
	protected void preDelete(ValidationSession entity) {
		
	}

	@Override
	protected void preSave(ValidationSession entity) {
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ValidationSessionManager#doGenerateUrlByEmail(java.lang.String, java.lang.Short)
	 */
	public ValidationSession doGenerateUrlByEmail(String email, Short type) {

		CustomerManager	customerManager = (CustomerManager)ContextUtil.getSpringBeanById("customerManager");
		
		if (!customerManager.getIsEmailExist(email))
			return null;

		// UUIDGenerator generator = UUIDGenerator.getInstance();

		String uuid = UUID.randomUUID().toString();

		Calendar calendar = Calendar.getInstance();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

		String currentTime = sdf.format(calendar.getTime());

		String originalText = uuid + currentTime + email;
		// use md5 to genrate a unique uid
		String uid = TranslateService.md5Translate(originalText);

		ValidationSession vs = new ValidationSession();
		// PasswordRecover pr=new PasswordRecover();

		vs.setEmail(email);
		// hourInterval
		int hourInterval = 2;
		try {
			hourInterval = ConfigUtil.getInstance().getValidationSessionTime();
		} catch (Exception ex) {
			hourInterval = 2;
		}

		// add a time intreval.
		calendar.add(Calendar.HOUR_OF_DAY, hourInterval);

		vs.setExpiredDate(calendar.getTime());

		vs.setUrl(uid);

		vs.setVsType(type);

		validationSessionDao.save(vs);

		return vs;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ValidationSessionManager#isUrlValid(java.lang.String, java.lang.String, java.lang.Short)
	 */
	public boolean isUrlValid(String email, String url, Short type) {
		return validationSessionDao.isUrlValid(email, url, type);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ValidationSessionManager#isUrlValid(java.lang.String, java.lang.Short)
	 */
	public boolean isUrlValid(String url, Short type) {
		return validationSessionDao.isUrlValid(url, type);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ValidationSessionManager#getByUrlType(java.lang.String, java.lang.Short)
	 */
	public ValidationSession getByUrlType(String url, Short type) {
		return validationSessionDao.getByUrlType(url, type);
	}

	public void setValidationSessionDao(ValidationSessionDao validationSessionDao) {
		this.validationSessionDao = validationSessionDao;
	}
}
